package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.FunctionDAO;
import cn.edu.scau.cmi.hombio.dao.RoleAuthorityDAO;

import cn.edu.scau.cmi.hombio.domain.Function;
import cn.edu.scau.cmi.hombio.domain.RoleAuthority;

import cn.edu.scau.cmi.hombio.service.FunctionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Function entities
 * 
 */

@Controller("FunctionRestController")

public class FunctionRestController {

	/**
	 * DAO injected by Spring that manages Function entities
	 * 
	 */
	@Autowired
	private FunctionDAO functionDAO;

	/**
	 * DAO injected by Spring that manages RoleAuthority entities
	 * 
	 */
	@Autowired
	private RoleAuthorityDAO roleAuthorityDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Function entities
	 * 
	 */
	@Autowired
	private FunctionService functionService;

	/**
	 * Save an existing RoleAuthority entity
	 * 
	 */
	@RequestMapping(value = "/Function/{function_id}/roleAuthorities", method = RequestMethod.PUT)
	@ResponseBody
	public RoleAuthority saveFunctionRoleAuthorities(@PathVariable Integer function_id, @RequestBody RoleAuthority roleauthorities) {
		functionService.saveFunctionRoleAuthorities(function_id, roleauthorities);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthorities.getId());
	}

	/**
	* Save an existing Function entity
	* 
	*/
	@RequestMapping(value = "/Function", method = RequestMethod.PUT)
	@ResponseBody
	public Function saveFunction(@RequestBody Function function) {
		functionService.saveFunction(function);
		return functionDAO.findFunctionByPrimaryKey(function.getId());
	}

	/**
	* Delete an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}/roleAuthorities/{roleauthority_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFunctionRoleAuthorities(@PathVariable Integer function_id, @PathVariable Integer related_roleauthorities_id) {
		functionService.deleteFunctionRoleAuthorities(function_id, related_roleauthorities_id);
	}

	/**
	* Create a new RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}/roleAuthorities", method = RequestMethod.POST)
	@ResponseBody
	public RoleAuthority newFunctionRoleAuthorities(@PathVariable Integer function_id, @RequestBody RoleAuthority roleauthority) {
		functionService.saveFunctionRoleAuthorities(function_id, roleauthority);
		return roleAuthorityDAO.findRoleAuthorityByPrimaryKey(roleauthority.getId());
	}

	/**
	* Show all Function entities
	* 
	*/
	@RequestMapping(value = "/Function", method = RequestMethod.GET)
	@ResponseBody
	public List<Function> listFunctions() {
		return new java.util.ArrayList<Function>(functionService.loadFunctions());
	}

	/**
	* Create a new Function entity
	* 
	*/
	@RequestMapping(value = "/Function", method = RequestMethod.POST)
	@ResponseBody
	public Function newFunction(@RequestBody Function function) {
		functionService.saveFunction(function);
		return functionDAO.findFunctionByPrimaryKey(function.getId());
	}

	/**
	* Delete an existing Function entity
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFunction(@PathVariable Integer function_id) {
		Function function = functionDAO.findFunctionByPrimaryKey(function_id);
		functionService.deleteFunction(function);
	}

	/**
	* Select an existing Function entity
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}", method = RequestMethod.GET)
	@ResponseBody
	public Function loadFunction(@PathVariable Integer function_id) {
		return functionDAO.findFunctionByPrimaryKey(function_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Show all RoleAuthority entities by Function
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}/roleAuthorities", method = RequestMethod.GET)
	@ResponseBody
	public List<RoleAuthority> getFunctionRoleAuthorities(@PathVariable Integer function_id) {
		return new java.util.ArrayList<RoleAuthority>(functionDAO.findFunctionByPrimaryKey(function_id).getRoleAuthorities());
	}

	/**
	* View an existing RoleAuthority entity
	* 
	*/
	@RequestMapping(value = "/Function/{function_id}/roleAuthorities/{roleauthority_id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleAuthority loadFunctionRoleAuthorities(@PathVariable Integer function_id, @PathVariable Integer related_roleauthorities_id) {
		RoleAuthority roleauthority = roleAuthorityDAO.findRoleAuthorityByPrimaryKey(related_roleauthorities_id, -1, -1);

		return roleauthority;
	}
}